<?
if( isset($_GET['a']) ){
	$a=$_GET['a']; /*b:buscar, g:grafico*/

	//buscar un producto	
	if($a=='b'){
		if( isset($_GET['s']) ){
			$s=$_GET['s'];
			$wk=$_SERVER['HTTP_USER_AGENT'];
			
			if( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']!='' ){
				$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
			}else{
				$ip=$_SERVER['REMOTE_ADDR'];
			}
			
			$cnn = new MySQLi('localhost','cecilsac_usuario','hostgator123.','cecilsac_sincupon');
			
			/*LOG DE APLICACION*/
			mysqli_query($cnn,'CALL INS_APPLOG(1,"'.$s.'|'.$wk.'|'.$ip.'");');	
			
			mysqli_multi_query($cnn,'call QRY_DETALLESUGERENCIAPRODUCTO('.$s.')');	
		
			$response='';
			$response->status='ok';
			
			/*Detalle producto*/
			$query_rs01 = mysqli_use_result($cnn) or die( '1-'.mysqli_error( $cnn ) );
			while($r = mysqli_fetch_assoc($query_rs01)){ 
				$response->detalle=$r;
			}
			
			/*Historial precios*/
			mysqli_next_result($cnn);
			$query_rs02 = mysqli_use_result($cnn) or die( '2-'.mysqli_error( $cnn ) );
			while($r = mysqli_fetch_assoc($query_rs02)){ 
				$response->precios[]=$r;
			}
		
			/*Sugerencia*/
			/*$query_rs03 = mysqli_next_result($query);*/
		
			echo json_encode($response);
		}
	}

	//generar grafico de precios
	if($a=='g'){
		include("chart/phplot.php");
		$w=$_GET['w'];
		$h=$_GET['h'];
		$d=$_GET['d'];
		$d=explode(',',$d);
		$graph = new PHPlot($w,$h);

		$chart_data = array();
		for($i=0;$i < count($d);$i++)
			array_push( $chart_data , array('',$d[$i]) );

		$graph->SetDataValues($chart_data);

		$graph->DrawGraph();
	}
	
	if($a=='i'){
			$mysqli=mysqli_connect('localhost','cecilsac_usuario','hostgator123.','cecilsac_sincupon');
			$stmt = $mysqli->prepare('SELECT ImagenProductoTienda FROM PRODUCTOTIENDA WHERE Cod_ProductoTienda=?'); 
			$stmt->bind_param("i", $s);
			
			$stmt->execute();
			$stmt->store_result();
			
			$stmt->bind_result($image);
			$stmt->fetch();
			
			header("Content-Type: image/jpeg");
			/************/
			$im = imagecreatefromstring($image);
			$new = imagecreatetruecolor($_GET['w'], $_GET['h']);
			$x = imagesx($im);
			$y = imagesy($im);
			imagecopyresampled($new, $im, 0, 0, 0, 0, $_GET['w'], $_GET['h'], $x, $y);
			imagedestroy($im);
			header('Content-type: image/jpeg');
			imagejpeg($new, null, 85);
			/************/
			//echo $image; 
	}
}
?>